package Reference;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class 独董意见20190101_20190430_下载pdf {
    static int sum=0;
    static OkHttpClient client = new OkHttpClient()
            .newBuilder().connectTimeout(60, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).build();
    static MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
    static MongoClientOptions options = builder.maxConnectionIdleTime(100000).build();
    static MongoClient mongoClient = new MongoClient("192.168.1.171:30000", options);
    static MongoCollection collection = mongoClient.getDatabase("JuChaoZiXun").getCollection("独董意见20190101_20190430");
    static FindIterable<Document> findIterable = collection.find().batchSize(100); //iterator——迭代
    static MongoCursor<Document> mongoCursor = findIterable.iterator();  //游标
    static Logger logger = LoggerFactory.getLogger(独董意见20190101_20190430_下载pdf.class);

    public static void main(String[] args) throws Exception {
        while (mongoCursor.hasNext()) {
            sum++;
            Document d = mongoCursor.next(); //遍历每一条数据
            System.out.println("正在读取数据库第-------"+sum+"条数据");
            String title_name="";
            title_name = d.getString("secCode") + "_"  + d.getString("title") + "_" + d.getString("secName") + "_" + d.getString("date") + "_" + d.getString("no")+ "_" + d.getString("Size")+ "_" + d.getInteger("i");
            title_name=title_name.replace("*", "").replace(":", "：").replace("\"", "").replace("</em>","").replace("<em>","").replace("<","（").replace(">","）");
            String download_url = "http://www.cninfo.com.cn/new/announcement/download?bulletinId=" + d.getString("no") + "&announceTime=" + d.getString("date");
            //标题与网址的键值对
            Map<String, String> title_url = new LinkedHashMap<String, String>();
            title_url.put(title_name, download_url);
            for (Map.Entry<String, String> empty : title_url.entrySet()) {
//            //download再一次请求
                Request request1 = new Request.Builder()
                        .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36")
                        .addHeader("Referer", "http://www.cninfo.com.cn/new/fulltextSearch?notautosubmit=&keyWord=%E7%8B%AC%E8%91%A3%E6%84%8F%E8%A7%81")
                        .addHeader("Host", "www.cninfo.com.cn")
                        .addHeader("Connection", "keep-alive")
                        .addHeader("X-Requested-With", "XMLHttpRequest")
                        .url(empty.getValue())
                        .build();
                String save_file = empty.getKey().split("_")[1];

//            System.out.println("下载文件---" + empty.getKey());
                client = new OkHttpClient()
                        .newBuilder().connectTimeout(60, TimeUnit.SECONDS)
                        .readTimeout(60, TimeUnit.SECONDS).build();
                client.dispatcher().setMaxRequests(10);
                client.dispatcher().setMaxRequestsPerHost(5);
                Download(client, request1, empty.getKey());
                Thread.sleep(3000);
                //int randint = (new Random()).nextInt(5);
                //Thread.sleep((randint+2)*200);
            }
        }
    }
    private static void Download(final OkHttpClient client, Request request,
                                 final String key) throws Exception {
        //Random random = new Random();
        //int randint=random.nextInt(5);
        //Thread.sleep((randint+2)*200);

        int count=0;
        final Call call=client.newCall(request);
        Response response = null;
        try{
            response = call.execute();
            while (true){
                if(response!=null&&response.isSuccessful()) {
                    byte[] data = response.body().bytes();
                    //org.apache.commons.io.FileUtils.writeByteArrayToFile(new File("I:\\阮琼玉\\独董意见\\独董意见20190101_20190430\\"+ key + ".pdf"), data);
                    System.out.println("----------已下载---"+key);
                }
                break;
            }
        }catch (Exception e){
            //Thread.sleep(2*1000);
            e.printStackTrace();
        }
    }


}
